import * as api from './api';
import {
    getCurrentUserPagePermissionsApi,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsSuccess
} from 'Shared/actions/currentUserContext';
import {PAGE_CODE} from './constants';
import {conventTableCondition} from '../utils';

/*==============================初始化API================================= */
export const GET_INIT_BEGIN = 'GET_INIT_BEGIN';
export const GET_INIT_SUCCESS = 'GET_INIT_SUCCESS';
export const GET_INIT_FAIL = 'GET_INIT_FAIL';
export const getInitDataBegin = () => ({
    type: GET_INIT_BEGIN
});
export const getInitDataSuccess = data => ({
    type: GET_INIT_SUCCESS,
    data,
});
export const getInitDataFail = message => ({
    type: GET_INIT_FAIL,
    message
});

//获取初始化数据和权限
export const getInitData = (code = PAGE_CODE) => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.initQuery().then(res => {
        if(res.ok)
            dispatch(getInitDataSuccess(res.data));
        return res.ok;
    });
    const permissions = getCurrentUserPagePermissionsApi(code).then(res => {
        if(res.isOk)
            dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else
            dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });
    return Promise.all([init, permissions]).then(values => values[0] && values[1]);
};

/*==============================查询列表================================= */
export const GET_LIST_BEGIN = 'GET_LIST_BEGIN';
export const GET_LIST_SUCCESS = 'GET_LIST_SUCCESS';
export const GET_LIST_FAIL = 'GET_LIST_FAIL';

export const getListBegin = () => ({
    type: GET_LIST_BEGIN,
});
export const getListSuccess = (data, condition) => ({
    type: GET_LIST_SUCCESS,
    data,
    condition,
});
export const getListFail = () => ({
    type: GET_LIST_FAIL
});

//获取列表
export const getList = condition => dispatch => {
    if(!condition)
        return;
    const queryCondition = conventTableCondition(condition);
    const {createTime, ...props} = queryCondition;
    if(createTime && createTime.length > 0) {
        props.beginCreateTime = createTime[0];
        props.endCreateTime = createTime[1];
    }
    dispatch(getListBegin());
    return api.getList(props).then(res => {
        if(res.ok)
            dispatch(getListSuccess(res.data, condition));
        else
            dispatch(getListFail());
    });
};
//按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getList(condition));
};
//分页排序搜索
export const tableSearch = page => (dispatch, getState) => {
    const query = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

/*=============================获取详情================================== */
export const GET_DETAIL_BEGIN = 'GET_DETAIL_BEGIN';
export const GET_DETAIL_SUCCESS = 'GET_DETAIL_SUCCESS';
export const GET_DETAIL_FAIL = 'GET_DETAIL_FAIL';
export const getDetailBegin = () => ({
    type: GET_DETAIL_BEGIN,
});

export const getDetailSuccess = data => ({
    type: GET_DETAIL_SUCCESS,
    data,
});

const getDetailFail = () => ({
    type: GET_DETAIL_FAIL,
});

//获取详情
export const getDetail = id => dispatch => {
    dispatch(getDetailBegin());
    api.getDetail(id).then(res => {
        if(res.ok)
            dispatch(getDetailSuccess(res.data));
        else
            dispatch(getDetailFail());
    });
};

/*==============================查询action================================= */
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//选择查询条件
export const SAVE_QUERY_DEALER = 'SAVE_QUERY_DEALER';
export const saveQueryDealer = data => ({
    type: SAVE_QUERY_DEALER,
    data
});
/*==============================重置查询条件================================= */
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL
});

/*==============================新增修改action================================= */
export const onDeclareSubmit = (id, data) => (dispatch, getState) => {
    const detail = getState().getIn(['page', 'domainData', 'detail', 'data']).toJS();
    const submitData = getState().getIn(['page', 'domainData', 'submitData']).toJS();
    //增加判断必填字段
    return api.onDeclareSubmit(id, {
        attachments: submitData.declareAttachments,
        rowVersion: detail.rowVersion
    }).then(res => ({success: res.ok}));
};

export const onClearanceSubmit = (id, data) => (dispatch, getState) => {
    const detail = getState().getIn(['page', 'domainData', 'detail', 'data']).toJS();
    const submitData = getState().getIn(['page', 'domainData', 'submitData']).toJS();
    //增加判断必填字段
    return api.onClearanceSubmit(id, {
        attachments: submitData.clearanceAttachments,
        rowVersion: detail.rowVersion
    }).then(res => ({success: res.ok}));
};

export const UPDATE_ATTACHMENTS = 'UPDATE_ATTACHMENTS';
export const updateAttachments = data => ({
    type: UPDATE_ATTACHMENTS,
    data: {
        fileId: data.id,
        fileName: data.fileName
    }
});

export const REMOVE_ATTACHMENTS = 'REMOVE_ATTACHMENTS';
export const removeAttachments = id => ({
    type: REMOVE_ATTACHMENTS,
    id
});

export const UPDATE_ATTACHMENTS_CLEAR = 'UPDATE_ATTACHMENTS_CLEAR';
export const updateAttachmentsClear = data => ({
    type: UPDATE_ATTACHMENTS_CLEAR,
    data: {
        fileId: data.id,
        fileName: data.fileName
    }
});

export const REMOVE_ATTACHMENTS_CLEAR = 'REMOVE_ATTACHMENTS_CLEAR';
export const removeAttachmentsClear = id => ({
    type: REMOVE_ATTACHMENTS_CLEAR,
    id
});
